package com.iims.supplier;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.constant.UserConstants;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.supplier.domain.SupplierType;
import com.iims.supplier.service.ISupplierTypeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/11 14:33
 */
@Api(tags = "F-供应商类型管理")
@RestController
@RequestMapping("/business/supplier/type")
public class SupplierTypeController extends BaseController {
    @Autowired
    private ISupplierTypeService supplierTypeService;

    /**
     * 获取供应商类型列表
     */
    @ApiOperation("分页查询供应商类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "供应商类型名称", name = "supTypeName", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:supplierType:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore SupplierType supplierType) throws Exception {
        startPage();
        List<SupplierType> list = supplierTypeService.selectSupplierTypeList(supplierType);
        return getDataTable(list);
    }

    /**
     * 根据供应商类型编号获取详细信息
     */
    @Deprecated
    //@ApiOperation("根据供应商类型编号获取详细信息")
    //@ApiImplicitParam(name = "supTypeId", value = "供应商类型Id", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('business:supplierType:query')")
    @GetMapping(value = "/{supTypeId}")
    public AjaxResult getInfo(@PathVariable Long supTypeId) {
        return AjaxResult.success(supplierTypeService.selectOne(supTypeId));
    }

    /**
     * 新增供应商类型
     */
    @ApiOperation("新增供应商类型")
    @ApiOperationSupport(ignoreParameters = {"supTypeId"})
    @PreAuthorize("@ss.hasPermi('business:supplierType:add')")
    @Log(title = "供应商类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SupplierType supplierType) {
        if (UserConstants.NOT_UNIQUE.equals(supplierTypeService.checkSupNameUnique(supplierType))) {
            return AjaxResult.error("新增供应商类型'" + supplierType.getSupTypeName() + "'失败，供应商类型名称已存在");
        }
        return toAjax(supplierTypeService.insertSupplierType(supplierType));
    }

    /**
     * 修改供应商类型
     */
    @ApiOperation("修改供应商类型")
    @PreAuthorize("@ss.hasPermi('business:supplierType:edit')")
    @Log(title = "供应商类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SupplierType supplierType) {
        if (UserConstants.NOT_UNIQUE.equals(supplierTypeService.checkSupNameUnique(supplierType))) {
            return AjaxResult.error("修改供应商类型'" + supplierType.getSupTypeName() + "'失败，供应商类型名称已存在");
        }
        return toAjax(supplierTypeService.updateSupplierType(supplierType));
    }

    /**
     * 删除供应商类型
     */
    @ApiOperation("删除供应商类型")
    @PreAuthorize("@ss.hasPermi('business:supplierType:remove')")
    @Log(title = "供应商类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supTypeId}")
    public AjaxResult remove(@ApiParam("供应商类型Id") @PathVariable Long supTypeId) {
        if (supplierTypeService.checkSupplierTypeUse(supTypeId)) {
            return AjaxResult.error("删除供应商类型失败，供应商类型已使用！请先删除关联供应商类型的供应商！");
        }
        return toAjax(supplierTypeService.deleteSupplierTypeById(supTypeId));
    }
}
